﻿Namespace Functions
    Public Class MetisInputFunction
        Inherits MetisFunction

        Sub New(ByVal MetisCore As MetisCore, Optional ByVal [Namespace] As MetisNamespace = Nothing)
            _MetisCore = MetisCore
            _Namespace = [Namespace]
            _ID = _MetisCore.GetTemporaryID
            _Name = String.Format(My.Resources.Metis_Dim_NewName.ToString, _ID * -1)
            _Table = New DB.DBTable(_MetisCore, "", DB.DBTableType.Table)
            _IsModified = True
            _LoadState = MetisLoadState.NewDefinition
        End Sub


        Sub New(ByVal MetisCore As MetisCore, ByVal [Namespace] As MetisNamespace, ByVal ID As Integer, ByVal Name As String, ByVal Annotation As String, _
                ByVal Table As DB.DBTable, ByVal ModifiedOn As Date, ByVal CreatedOn As Date)

            _MetisCore = MetisCore
            _Namespace = [Namespace]
            _UsedByFunctions = New MetisFunctionList(_MetisCore, Me)
            _UsesFunctions = New MetisFunctionList(_MetisCore, Me)
            _ID = ID
            _Name = Name
            _BackupName = Name
            _Annotation = Annotation
            _BackupAnnotation = Annotation
            _Table = Table
            _ModifiedOn = ModifiedOn
            _CreatedOn = CreatedOn
            _Level = 0
            _LoadState = MetisLoadState.NotLoaded
        End Sub

        Public Overrides Sub Reload()
            If _LoadState <> MetisLoadState.NewDefinition Then
                Dim OraReader As OracleDataReader
                Dim OraCmd As OracleCommand

                _LoadState = MetisLoadState.Loading

                'Dimension base information
                OraCmd = New OracleCommand(String.Format("SELECT name, table_name, description, modified_on, created_on " & _
                                                         "FROM dam_functions where function_id={0}", _ID), _MetisCore.Connection)
                OraReader = OraCmd.ExecuteReader
                If OraReader.Read Then
                    _Name = OraReader.GetString(0)
                    _Table = _MetisCore.DBTables.FindTable(OraReader.GetString(1))
                    If _Table Is Nothing Then _Table = New DB.DBTable(_MetisCore, OraReader.GetString(1), DB.DBTableType.Table)

                    _Annotation = If(OraReader.IsDBNull(2), "", OraReader.GetString(2))

                    _ModifiedOn = OraReader.GetDateTime(3)
                    _CreatedOn = OraReader.GetDateTime(4)

                    _BackupName = _Name
                    _Annotation = _BackupAnnotation

                    OraReader.Close()

                    'Fields
                    OraCmd = New OracleCommand(String.Format("SELECT function_field_id, field_name, field_type, db_field_name, expression, description, " & _
                                                             "dimension_fk, parent_field, modified_on, created_on, number_format, NVL(column_width,64) " & _
                                                             "FROM dam_function_fields WHERE function_fk={0} ORDER BY seq", _ID), _MetisCore.Connection)
                    OraReader = OraCmd.ExecuteReader
                    _Fields.Clear()
                    _BackupFields.Clear()
                    Do While OraReader.Read

                        Dim tmpFieldExpr As String = If(OraReader.IsDBNull(4), "", OraReader.GetString(4))
                        Dim tmpFieldAnnot As String = If(OraReader.IsDBNull(5), "", OraReader.GetString(5))
                        Dim tmpFieldDim As Dimensions.MetisDimension = If(OraReader.IsDBNull(6), Nothing, _MetisCore.Dimensions.Find(OraReader.GetInt32(6)))
                        Dim tmpFieldParent As Integer = If(OraReader.IsDBNull(7), -1, OraReader.GetInt32(7))
                        Dim tmpFieldFormat As String = If(OraReader.IsDBNull(10), "", OraReader.GetString(10))

                        Dim tmpField As New MetisFunctionField(Me, OraReader.GetInt32(0), OraReader.GetString(1), OraReader.GetInt32(2), _
                                                                  OraReader.GetString(3), tmpFieldExpr, tmpFieldAnnot, tmpFieldDim, tmpFieldParent, _
                                                                  OraReader.GetDateTime(8), OraReader.GetDateTime(9), _
                                                                  tmpFieldFormat, OraReader.GetInt32(11))
                        _Fields.Add(tmpField)
                        _BackupFields.Add(tmpField.Clone)
                    Loop

                    'Uses
                    'OraCmd = New OracleCommand(String.Format("SELECT DISTINCT uses_function_fk FROM dam_function_ref " & _
                    '                                         "WHERE function_fk={0}", _ID), _MetisCore.Connection)
                    'OraReader = OraCmd.ExecuteReader
                    '_UsesFunctions.Clear()
                    'Do While OraReader.Read
                    '    Dim tmpFunc As Core.Functions.MetisFunction = _MetisCore.Functions.Find(OraReader.GetInt32(0))
                    '    If tmpFunc IsNot Nothing Then _UsesFunctions.Add(tmpFunc)
                    'Loop

                    'UsedBy
                    OraCmd = New OracleCommand(String.Format("SELECT DISTINCT function_fk FROM dam_function_ref " & _
                                                             "WHERE uses_function_fk={0}", _ID), _MetisCore.Connection)
                    OraReader = OraCmd.ExecuteReader
                    _UsedByFunctions.Clear()
                    Do While OraReader.Read
                        Dim tmpFunc As Core.Functions.MetisFunction = _MetisCore.Functions.Find(OraReader.GetInt32(0))
                        If tmpFunc IsNot Nothing Then _UsedByFunctions.Add(tmpFunc)
                    Loop

                End If
                OraReader.Close()

                _LoadState = MetisLoadState.Loaded
                _IsModified = False
                _LastUpdate = Date.Now
                'RaiseEvent Loaded(Me)
            End If
        End Sub

        Public Overrides Sub Save()

        End Sub

        Public Overrides ReadOnly Property FunctionType() As MetisFunctionType
            Get
                Return MetisFunctionType.Input
            End Get
        End Property

        Public Overrides Sub CheckIsModified()

        End Sub
    End Class
End Namespace